import request from '@/utils/request'

//登录方法
export function login(data) {
  return request({
    url: '/user/api/auth/login',
    method: 'post',
    data: data
  })
}

//退出登录方法
export function logout() {
  return request({
    url: '/user/api/auth/logout',
    method: 'post'
  })
}  

// 注册方法
export function register(data) {
  return request({
    url: '/user/api/auth/register',
    method: 'post',
    data: data
  })
}

//发送验证码(暂未使用)
export function sendVerificationCode(data) {
  return request({
    url: '/captchaImage',
    method: 'post',
    data: data
  })
}

// 根据用户id获取用户信息
export  function getUserInfo(userId) {
  return request({
    url: `/user/api/users/${userId}`,
    method: 'get'
  })
}

// 根据用户id更新用户信息
export async function updateUserInfo(userId, data) {
  return request({
    url: `/user/api/users/${userId}`,
    method: 'put',
    data
  })
}
//上传用户头像
export async function uploadAvatar(userId, file) {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: `/user/api/users/${userId}/avatar`,
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 获取公告列表
export async function getNoticeList(page = 0, size = 10) {
  return request({
    url: `/user/api/notices?page=${page}&size=${size}`,
    method: 'get'
  })
}

// 获取用户列表，支持关键字、分页、排序
export async function getUserList({ keyword = '', page = 0, size = 10, sort = 'createdAt,desc' } = {}) {
  // 拼接参数
  let params = []
  if (keyword) params.push(`keyword=${encodeURIComponent(keyword)}`)
  if (page !== undefined) params.push(`page=${page}`)
  if (size !== undefined) params.push(`size=${size}`)
  if (sort) params.push(`sort=${sort}`)
  const paramStr = params.length ? '?' + params.join('&') : ''
  return request({
    url: `/user/api/users${paramStr}`,
    method: 'get'
  })
}

// 激活账号
export function activateUser(userId) {
  return request({
    url: `/user/api/users/${userId}/activate`,
    method: 'post'
  })
}

// 停用账号
export function deactivateUser(userId) {
  return request({
    url: `/user/api/users/${userId}/deactivate`,
    method: 'post'
  })
}

// 批量激活账号
export function batchActivateUser(userIds) {
  return request({
    url: '/user/api/users/batch-activate',
    method: 'post',
    data: userIds // 传递为数组，如 [1,2,3]
  })
}

// 获取好友列表
export function getFriendList(userId) {
  return request({
    url: `/user/api/relations/${userId}/friends`,
    method: 'get'
  })
}

// 添加好友
export function addFriend(userId, friendId) {
  return request({
    url: `/user/api/relations/${userId}/friends/${friendId}`,
    method: 'post'
  })
}

// 删除好友
export function removeFriend(userId, friendId) {
  return request({
    url: `/user/api/relations/${userId}/remove/${friendId}`,
    method: 'post'
  })
}

// 拉黑用户
export function blockUser(userId, blockUserId) {
  return request({
    url: `/user/api/relations/${userId}/block/${blockUserId}`,
    method: 'post'
  })
}